<?php
namespace Home\Model;
use Think\Model;

class AdModel extends Model{
    
    /**
     * @todo 获取广告
     * @param array $postion_id 广告位置
     * @return  array
     * @author fzh
     */
    public function getAd($postion_id){
        $obj = D('Admin/ServiceApi');
        $postion_info = $obj->getAdPostion($postion_id);

        if(empty($postion_info)){
            $this->error = '位置信息不存在';
            return false;
        }
        $ad_list = $obj->getAdList($postion_id);

        if(empty($ad_list)){
            return array();
        }
        
        foreach ($postion_info as $key => $val){
            $ad_position_id = $val['ad_position_id'];
            $height = $val['ad_position_height'];
            $width = $val['ad_position_width'];
            $limit = $val['ad_position_limit'];
            switch ($val['ad_position_type']){
                case 1: //自定义
                    $data [$key] = '调错接口';
                    break;
                case 2: //图片
                    $data [$key] = $this->getPictureAd($ad_list[$ad_position_id],$width,$height,$limit);
                    break;
                case 3: //文字
                    $data [$key] = $this->getWordsAd($ad_list[$ad_position_id],$limit);
                    break;
            }
        }
        return $data;
    }
    
    /**
     * @todo 图片广告
     * @param array $ad_list 广告信息
     * @return  string
     * @author fzh
     */
    protected function getPictureAd($ad_list,$width,$height,$limit){
        if(empty($ad_list)){
            return '';
        }
        $data = '';
        foreach ($ad_list as $val){
            $src = getPictureThumb($val['pic_id'],$width,$height);
            $data .= '<a href="'.$val['ad_link'].'" target="_blank">'
                    .'<img src="'.$src.'" width="'.$width.'" height="'.$height.'" /></a>';
        }
        return $data;
    }
    
    /**
     * @todo 文字广告
     * @param array $ad_list 广告信息
     * @return  string
     * @author fzh
     */
    protected function getWordsAd($ad_list,$width,$height,$limit){
        if(empty($ad_list)){
            return '';
        }
        $data = '';
        foreach ($ad_list as $val){
            $data .= '<a href="'.$val['ad_link'].'" target="_blank">'.$val['ad_name'].'</a>';
        }
        return $data;
    }
    
    
    
}